<?php

define("ACCESS_LEVEL_PRIVATE"            , 1);
define("ACCESS_LEVEL_USER_DEFINED"       , 2);
define("ACCESS_LEVEL_FRIENDS"            , 3);
define("ACCESS_LEVEL_FRIENDS_OF_FRIENDS" , 4);
define("ACCESS_LEVEL_PUBLIC"             , 5);
//include_once "sportiw_core.php";

class plugins extends bee\singleton
{

  protected $wpCountRows;
  protected $me;
  public    $isDebugMode;
  public    $canCreateVirtualProfile;
  public    $language;
  public    $package;
  public    $packageName;
  public    $defaultVisibility;
  protected static $__instance;

  protected function __construct()
  { $this->me                  = bee\app::getInstance()->getUser()->id;
    $this->isDebugMode         = false;
    $this->language            = "en";
  }

  public function preProcess($evt)
  { //echo ":PPrP:";
  }

  public function preForm($evt)
  { //echo ":PPrF:";
  }


  public function postForm($evt)
  { //echo "PPoF";
    $DSDoc                     = bee\XMLDataHub::getInstance();
    $DSDoc->setVariable("__isDebugMode", $this->isDebugMode);
    $DSDoc->setVariable("__language", $this->language);
    $DSDoc->setVariable("canCreateVirtualProfile", $this->canCreateVirtualProfile);
    $DSDoc->setVariable("package", $this->package);
    $DSDoc->setVariable("packageName", $this->packageName);
    $DSDoc->setVariable("defaultVisibility", $this->defaultVisibility);
  }

  public function postProcess($evt)
  { $app                       = bee\app::getInstance();

    //$echo                      = $app->echo;

    if($langTranslations = parse_ini_file(bee\config::getInstance()->sys_APP_PATH . "/conf/lang/" . $this->language . ".ini"))
    $app->echo                      = strtr($app->echo, $langTranslations);


    $debugVarsArr = $app->getDebugVars();
    $timePoints = $app->getTimePoints();

    foreach ($debugVarsArr as $key => &$value)
    {
      if(is_object($value) || is_array($value))
      { $value = print_r($value, true);
      }
    	$value = "$key: $value";
    }
    foreach ($timePoints as $key => &$value)
    {
      $value = "$key: " . round($value, 3);
    }

    $app->echo                      = str_replace("__bee__debug__",
     "<h4>Debug console:</h4>
      <table width=\"100%\" style=\"cursor: pointer; min-width:350px;\">
      <tr><td id=\"varz\">varz</td><td id=\"echoez\">echoez</td><td id=\"timez\">timez</td></tr>
      </table>
      <hr />
      <div id=\"divVarz\">varz:<br />" . implode("<br />", $debugVarsArr) . "</div>
      <div id=\"divEchoez\">echoz:<br />" . $app->getDebug() . "</div>
      <div id=\"divTimez\">timez:<br />" . implode("<br />", $timePoints) . "</div>
      <hr />
        <br /><strong>Memory usage: </strong>" . round(memory_get_peak_usage(true)/1048576, 3) . " MB
        <br /><strong>Total time: </strong>" . round($app->getExecutionTime(), 3) . " s
        <br /><br /><strong>Debug info: </strong><br/>
          <script type=\"text/javascript\">
          $(document).ready(function()
          {
            $('#divVarz').show();
            $('#divEchoez').hide();
            $('#divTimez').hide();

            $('#varz').click(function()
            {
              $('#divVarz').show();
              $('#divEchoez').hide();
              $('#divTimez').hide();
            })
            $('#echoez').click(function()
            {
              $('#divVarz').hide();
              $('#divEchoez').show();
              $('#divTimez').hide();
            })
            $('#timez').click(function()
            {
              $('#divVarz').hide();
              $('#divEchoez').hide();
              $('#divTimez').show();
            })
          })
          </script>", $app->echo);
  }

  public function xxx__countRows($evt)
  {
    $db                  = bee\db::getInstance();
    switch ($evt["counter"])
    {
      case "friends":
        $AW          = new bee\afterWhere( "SELECT Count(*) FROM friends WHERE (user1 = :user1 AND type1 = :type1
                                                                            OR user2 = :user2 AND type2 = :type2)
                                                                            AND status = :status",
                                                                              array(  ":user1" => $this->me,             ":user2" => $this->me,
                                                                              ":type1" => FRIENDS_TYPE_FRIENDS, ":type2" => FRIENDS_TYPE_FRIENDS,
                                                                              ":status" => REQUEST_STATUS_ACCEPTED) );
        die($db->execSQL($AW));
        break;

      case "business":
        $AW          = new bee\afterWhere( "SELECT Count(*) FROM friends WHERE (user1 = :user1 AND type1 = :type1
                                                                            OR user2 = :user2 AND type2 = :type2)
                                                                            AND status = :status",
                                                                              array(  ":user1" => $this->me,             ":user2" => $this->me,
                                                                              ":type1" => FRIENDS_TYPE_BUSINESS, ":type2" => FRIENDS_TYPE_BUSINESS,
                                                                              ":status" => REQUEST_STATUS_ACCEPTED) );
        echo $db->execSQL($AW);
        break;

      case "teammates":
        $AW          = new bee\afterWhere( "SELECT Count(*) FROM friends WHERE (user1 = :user1 AND type1 = :type1
                                                                            OR user2 = :user2 AND type2 = :type2)
                                                                            AND status = :status",
                                                                              array(  ":user1" => $this->me,             ":user2" => $this->me,
                                                                              ":type1" => FRIENDS_TYPE_TEAMMATES, ":type2" => FRIENDS_TYPE_TEAMMATES,
                                                                              ":status" => REQUEST_STATUS_ACCEPTED) );
        die($db->execSQL($AW));
        break;

      case "messages":
      case "messages2":
        die("žžž");
        $m                     = new message();
        $AW                    = new bee\afterWhere("SELECT Count(*) FROM message WHERE `to` = :to AND status_to = :status AND type = :type",
        array(":to" => $this->me, ":status" => MSG_STATUS_UNREAD, ":type" =>MSG_TYPE_MESSAGE));
        die($db->execSQL($AW));
        break;

      case "requests":
      case "requests2":
        $m                     = new message();
        $AW                    = new bee\afterWhere("SELECT Count(*) FROM message WHERE `to` = :to AND status_to = :status AND type = :type",
        array(":to" => $this->me, ":status" => MSG_STATUS_UNREAD, ":type" =>MSG_TYPE_REQUEST));
        die($db->execSQL($AW));
        break;

      case "notifications":
      case "notifications2":
        $m                     = new message();
        $AW                    = new bee\afterWhere("SELECT Count(*) FROM message WHERE `to` = :to AND status_to = :status AND type = :type",
            array(":to" => $this->me, ":status" => MSG_STATUS_UNREAD, ":type" =>MSG_TYPE_NOTIFICATION));
        die($db->execSQL($AW));
        break;

      case "unreadMessagess":
        $m                     = new message();
        $AW                    = new bee\afterWhere("SELECT Count(*) FROM message WHERE `to` = :to AND status_to = :status AND type = :type",
        array(":to" => $this->me, ":status" => MSG_STATUS_UNREAD, ":type" =>MSG_TYPE_MESSAGE));
        if($nr = $db->execSQL($AW))
        {  die($db->execSQL($AW));
        }
        break;

      case "notes":
        $AW                    = new bee\afterWhere("SELECT Count(*) FROM note WHERE `owner` = :owner AND `class` = :class AND `item` = :item",
        array(":owner" => $this->me, ":class" => $evt["class"], ":item" => $evt["item"]));

        if($nr = $db->execSQL($AW))
        { die("<div class=\"div_number\">" . $db->execSQL($AW) . "</div>");
        }
        break;
      case "tags":
          $AW                    = new bee\afterWhere("SELECT Count(*) FROM tag WHERE `owner` = :owner AND `class` = :class AND `item` = :item",
          array(":owner" => $this->me, ":class" => $evt["class"], ":item" => $evt["item"]));

          die("<div class=\"div_number\">" . $db->execSQL($AW) . "</div>");
          break;

      case "allUserNotes":
            $AW                    = new bee\afterWhere("SELECT Count(*) FROM note WHERE `owner` = :owner AND `user` = :user",
            array(":owner" => $this->me, ":user" => $evt["user"]));

            if($nr = $db->execSQL($AW))
            { die("<div class=\"div_number\">" . $db->execSQL($AW) . "</div>");
            }
            break;
            case "allUserTags":
              $AW                    = new bee\afterWhere("SELECT Count(*) FROM tag WHERE `owner` = :owner AND `user` = :user",
              array(":owner" => $this->me, ":user" => $evt["user"]));

              if($nr = $db->execSQL($AW))
              { die("<div class=\"div_number\">" . $db->execSQL($AW) . "</div>");
              }
              break;

      case "allUserDocsByType":
              $AW                    = new bee\afterWhere("  SELECT Count(*)
                                                              FROM bee_document WHERE `owner` = :owner AND library
                                                              IN (SELECT id FROM bee_library WHERE parent=:type)",
              array(":owner" => $evt["owner"], ":type" => $evt["type"]));

              if($nr = $db->execSQL($AW))
              { die(" <div class=\"div_number\">" . $db->execSQL($AW) . "</div>");
              }
              break;


    }
  }
  public function setDebugMode($evt)
  {
    if($evt["debugMode"])
    { $this->isDebugMode = true;
    } else
    { $this->isDebugMode = false;
    }
  }

  public function setLanguage($evt)
  { $this->language = isset($evt["language"]) ? $evt["language"] : "en";
  }


  public function addTag($evt)
  { $tag                       = tag::addTag($evt["value"], $evt["class"], $evt["item"], ACCESS_LEVEL_PRIVATE, 0, $evt["user"]);

  }

  public function addNote($evt)
  { $note                      =  note::addNote($evt["value"], $evt["class"], $evt["item"], ACCESS_LEVEL_PRIVATE, $evt["user"]);
  //print_r($note);
  }



  public function getMyTags($evt)
  { $term = trim($evt["term"]);
    $tag = new tag();
    $tag->owner                = bee\app::getInstance()->getUser()->id;
  //    $tag->class                = $this->class;

    $rs = new bee\rowSet($tag, new bee\afterWhere(" AND value like :filter", array(":filter"=> $term . "%")), 0, 20);
    $list = array();
    while ($rs->getRow($tag))
    { $list[] = array("label" => $tag->value, "id" => $tag->value);
    }
    echo json_encode($list);
  }


  public function moveDocument($evt)
  {//die(print_r($evt));
  $document                  = new bee\document();
  $document->SELECT($evt["id"]);
  $document->library         = $evt["newLibrary"];
  $document->UPDATE();
  }

  public function getNotez($evt)
  {
    $note                      = new note();
    $note->class               = $evt["class"];
    $note->item                = $evt["item"];
    $note->owner               = bee\app::getInstance()->getUser()->id;
    $rs                        = new bee\rowSet($note);
    while($rs->getRow($note))
    {
      echo $note->value . "<brr />";
    }


  }

}